package com.balabala.gym_goods.service.impl;

import com.balabala.goods.pojo.dto.CartItemDTO;
import com.balabala.gym_goods.dao.CartDao;
import com.balabala.gym_goods.pojo.entity.Category;
import com.balabala.gym_goods.pojo.entity.Sku;
import com.balabala.gym_goods.pojo.entity.Spu;
import com.balabala.gym_goods.service.*;
import com.balabala.gym_goods.utils.MoneyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author 何涛
 * @Date: 2023/05/07/ 16:19
 * @description
 */
@Slf4j
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    CartDao cartDao;

    @Autowired
    SpuAttributeValueService spuAttributeValueService;

    @Autowired
    SkuService skuService;

    @Autowired
    SpuService spuService;

    @Autowired
    CategoryService categoryService;
    @Override
    public Boolean addSkuToCart(Long skuId) {
        //获取用户对应的购物车操作对象
        BoundHashOperations userCartOptions = cartDao.getUserCartOptions();
        CartItemDTO cartItemDTO = (CartItemDTO) userCartOptions.get(skuId);
        if(cartItemDTO==null){
            log.info("用户没有对应的sku订单项，新建一个,skuId:{}",skuId);
            Sku sku = skuService.getById(skuId);
            Spu spu = spuService.getById(sku.getSpuId());
            Category category= categoryService.getById(spu.getCategoryId());
            //获取这个sku的所有规格值
            String[] attributes = sku.getSpecIds().split(",");
            StringBuilder goodsName=new StringBuilder(sku.getSpuName());
            Arrays.stream(attributes).forEach(attId->goodsName.append("+").append(spuAttributeValueService.getById(attId).getAttributeValue()));
            cartItemDTO=CartItemDTO.builder().price(MoneyUtil.fenToYuan(sku.getPrice())).count(1)
                    .skuNo(sku.getNo()).skuPic(sku.getPicUrl()).skuId(skuId)
                    .stock(sku.getStock()).coupon(1L).checked(true).skuName(sku.getSpuName())
                    .spuId(sku.getSpuId())
                    .spuName(sku.getSpuName())
                    .categoryId(spu.getCategoryId())
                    .categoryName(category.getName())
                    .goodsName(goodsName.toString())
                    .build();
            //把这个购物项放入redis中
            // 可以用kafka改进

            cartDao.addSkuToCart(skuId,cartItemDTO);
            return true;
        }
        log.info("用户有对应的订单项，数量+1");
        cartItemDTO.setCount(cartItemDTO.getCount()+1);
        // 可以用kafka改进
        cartDao.addSkuToCart(skuId,cartItemDTO);
        return true;

    }

    @Override
    public List<CartItemDTO> getUserCart() {
        return cartDao.getUserCartItemList();
    }

    @Override
    public Boolean clearCart() {

        return cartDao.deleteUserCart();
    }

    @Override
    public Boolean updateCart(CartItemDTO cartItemDTO) {
        return cartDao.updateSkuToCart(cartItemDTO.getSkuId(), cartItemDTO);
    }

    @Override
    public Boolean deleteCartSku(Long skuId) {
        return cartDao.deleteCartItemBySkuId(skuId);
    }

    @Override
    public Boolean checkAllOrAllCancel(Boolean isCheckAll) {
        //获取用户的购物车
        Map<Long,CartItemDTO> userCartMap=cartDao.getUserCartEntries();
        Set<Long> skus = userCartMap.keySet();
        Iterator<Long> iterator = skus.iterator();
        while (iterator.hasNext())
        {
            Long skuId = iterator.next();
            CartItemDTO cartItemDTO = userCartMap.get(skuId);
            //修改所有订单项的选中状态为true
            cartItemDTO.setChecked(isCheckAll);

        }
        cartDao.batchUpdate(userCartMap);

        return true;
    }

    @Override
    public Boolean deleteCheckCartSkus(List<Long> skuIds) {
        skuIds.forEach(skuId->cartDao.deleteCartItemBySkuId(skuId));
        return true;
    }
}
